<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="150px">
    <!-- 显示头像 -->
    <!-- <el-form-item label="头像" prop="profilephoto">
      <el-avatar id="profilephoto" :size="80" :src="profilephoto" />
    </el-form-item> -->
    <el-form-item label="昵称" prop="name">
      <el-input v-model="name" />
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="age" />
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <!-- <el-input v-model="gender" /> -->
      <select v-model="gender">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </el-form-item>
    <el-form-item label="地址" prop="addr">
      <el-input v-model="addr" />
    </el-form-item>
    <el-form-item label="手机号码" prop="account">
      <el-input v-model="account" />
    </el-form-item>
    <el-form-item label="" prop="提交">
      <el-button type="primary" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import { updatePatientInfo } from '@/api/user'
// import { baseUrl } from '@/utils/request.js'
// const baseUrl = 'https://101.70.156.105:28847'

export default {
  data() {
    // // 模拟的假数据
    // const mockUserInfo = {
    //   user_id: '12345',
    //   age: 28,
    //   gender: 1, // 1或者0，假设1代表男性
    //   profilephoto: 'https://ts1.cn.mm.bing.net/th/user_id/R-C.57384e4c2dd256a755578f00845e60af?rik=uy9%2bvT4%2b7Rur%2fA&riu=http%3a%2f%2fimg06file.tooopen.com%2fimages%2f20171224%2ftooopen_sy_231021357463.jpg&ehk=whpCWn%2byPBvtGi1%2boY1sEBq%2frEUaP6w2N5bnBQsLWdo%3d&risl=&pid=ImgRaw&r=0', // 假头像链接
    //   name: 'John Doe',
    //   addr: 'abc',
    //   phone: '12312347890'
    //   // userAccount: 'johndoe123'
    // }

    // const equalToPassword = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请再次输入密码'))
    //   } else if (value !== this.form.userNewPassword) {
    //     callback(new Error('两次输入密码不一致!'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      // presentUser: {},
      form: {
        gender: ''
      },
      // profilephoto: '',
      patient_id: '',
      name: '',
      age: null,
      account: '',
      gender: '',
      addr: '',

      rules: {
        // name: [
        //   { required: true, message: '名称不能为空', trigger: 'blur' }
        // ],
      }
    }
  },

  mounted() {
    this.load()
  },
  methods: {
    // uploadAvatar() {
    //   // console.log('上传头像:', this.selectedFile);
    //   console.log('上传头像:', this.profilephoto)
    //   // 这里可以发送请求到后端，处理上传逻辑
    // },
    load() {
      // // 读取存储的 JSON 字符串并反序列化为对象
      const presentUser = JSON.parse(localStorage.getItem('presentUser'))
      this.form.gender = presentUser.gender
      this.patient_id = presentUser.user_id
      this.age = presentUser.age
      this.gender = presentUser.gender
      // this.profilephoto = presentUser.profilephoto ? presentUser.profilephoto : 'https://tse1-mm.cn.bing.net/th/id/OIP-C.8tlwbGsJvlNZwGAGL36W8AHaFS?rs=1&pid=ImgDetMain'
      this.addr = presentUser.addr
      this.name = presentUser.name
      this.account = presentUser.account
    },
    async submit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          const temp = {
            // user_id: this.user_id,
            // userPassword: this.form.userConfirmPassword,
            patient_id: this.patient_id,
            name: this.name,
            account: this.account,
            gender: this.gender,
            age: this.age,
            profilephoto: this.profilephoto,
            addr: this.addr
            // userAccount: this.form.userAccount,
          }
          const res = await updatePatientInfo(temp)
          if (res.message === '更新成功') {
            this.$message.success('保存成功')
            // this.dialogVisible = false
            localStorage.setItem('presentUser', JSON.stringify(temp))
            this.load()
          }
        } else {
          console.log('error submit!!')
          this.$message.error('保存失败')
        }
      })
    }
    // submit() {
    //   this.$message({
    //     message: 'User information has been updated successfully',
    //     type: 'success',
    //     duration: 5 * 1000
    //   })
    // }
    // handleImgSuccess (response, file, fileList) {
    //   console.log('Image uploaded successfully:', response)
    //   this.form.userImg = response.data
    // }
    // handleImgSuccess(response, file, fileList) {
    //   console.log('头像上传成功:', response)
    //   if (response && response.url) { // 假设服务器返回的数据结构是 {url: 'newImageUrl'}
    //     this.profilephoto = response.url
    //   } else {
    //     console.error('头像上传失败')
    //   }
    // }
  }
}
</script>
